package servlet;

import core.ServletBase;
import model.MyInstances;
import weka.CommonUtils;
import weka.core.Instances;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by taohj on 2015/1/7.
 */
@WebServlet("/preprocessServlet")
@MultipartConfig
public class PreprocessServlet extends ServletBase {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String contentType = req.getHeader("Content-Type");
        System.out.println("contentType:"+contentType);
        if (contentType!=null && contentType.indexOf("multipart") > -1) {
            upload(req, resp);
            return;
        }


        String action = req.getParameter("action");
        if (action == null || "".equals(action)) {
            throw new ServletException("参数action不能为空");
        }


        if ("input".equals(action)) {
            req.getRequestDispatcher("WEB-INF/page/tabs/preprocess/preprocess.jsp").forward(req, resp);
        } else {
            throw new ServletException("未找到参数action的对应项");
        }


    }


    private void upload(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {




        Part part = req.getPart("file");
        String filename = utils.CommonUtils.getFileName(part);
        String realPath = req.getServletContext().getRealPath("/") + "upload" + File.separator;
        String fileRealPath = realPath + req.getSession().getId() + filename.substring(filename.lastIndexOf("."));
        InputStream inputStream = part.getInputStream();

        File dir = new File(realPath);
        if (!dir.exists()) {
            dir.mkdir();
        }
        FileOutputStream outputStream = new FileOutputStream(new File(fileRealPath));
        byte[] buffer = new byte[1];
        int length = 0;
        while ((length = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, length);
        }
        outputStream.flush();
        outputStream.close();
        inputStream.close();

        super.setCurrentFilePath(req, fileRealPath);

        req.getSession().setAttribute("filename", filename);

        try {
            Instances instances = CommonUtils.getInstances(fileRealPath);
            MyInstances myInstances = CommonUtils.getMyInstances(instances);
            myInstances.setRelationName(filename);
            String printString = "<script>" + "\n";
            printString += "eval('var data=" + utils.CommonUtils.toJsonString(myInstances) + "');" + "\n";
            printString += "parent.context.preprocess.load(data)" + "\n";
            printString+="parent.ajaxComplete();";
            printString += "</script>" + "\n";
            print(resp, printString);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
